package com.fzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.common.model.personal.param.GetCompanyOrUserAllInfoParam;
import com.fzg.common.model.personal.param.UpdateLoginUserInfoParam;
import com.fzg.common.model.personal.result.GetCompanyOrUserAllInfoResult;
import com.fzg.common.tool.Medthods;
import com.fzg.common.tool.ToolForDateTime;
import com.fzg.entity.Enterprise;
import com.fzg.entity.User;
import com.fzg.mapper.EnterpriseMapper;
import com.fzg.mapper.UserMapper;
import com.fzg.service.PersonalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * create by fzg
 * 2022/3/15 19:23
 */

@Service
public class PersonalServiceImpl implements PersonalService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取当前登陆用户或者企业的全部信息
     *
     * @param param 参数类
     */
    @Override
    public ResponseWrapper getCompanyOrUserAllInfo(GetCompanyOrUserAllInfoParam param) {
        GetCompanyOrUserAllInfoResult result = new GetCompanyOrUserAllInfoResult();
        // 查询用户表
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccountNumber, param.getAccount()));
        if (user != null){
            if (user.getIsAdmin().equals(1)){
                result.setRemarks("管理员");
            }else {
                result.setRemarks("用户");
            }
            result.setUserAid(user.getAid());
            result.setUserAccount(user.getAccountNumber());
            if (user.getAvatar() != null){
                result.setAvatar(user.getAvatar());
            }else {
                result.setAvatar("https://recruitment-files.oss-cn-chengdu.aliyuncs.com/user/student1.jpg");
            }
            result.setUserName(user.getName());
            result.setUserSex(user.getGender()==1 ? "男" : "女");
            result.setUserIdNumber(Medthods.replaceIdNumberByStar(user.getIdentityNumber()));
            result.setUserIsMarried(user.getMaritalStatus()==0 ? "未婚" : "已婚");
            result.setUserPhone(user.getPhone());
            result.setUserEmail(user.getEmail());
            result.setUserRegisterDate(ToolForDateTime.StringDateToYearMonthDay(ToolForDateTime.LocalDateTimeToStringToDate(user.getCreateTime())));
            result.setUserAddress(user.getAddress());
        }else {
            Enterprise enterprise = enterpriseMapper.selectOne(new LambdaQueryWrapper<Enterprise>().eq(Enterprise::getAccountNumber, param.getAccount()));
            result.setRemarks("企业");
            result.setCompanyAid(enterprise.getAid());
            result.setAvatar(enterprise.getAvatar());
            result.setCompanyAccount(enterprise.getAccountNumber());
            result.setCompanyName(enterprise.getName());
            result.setCompanyAddress(enterprise.getAddress());
            result.setCompanyPhone(enterprise.getPhone());
            result.setCompanyEmail(enterprise.getEmail());
            result.setCompanyLeader(enterprise.getLeader());
            result.setCompanyRegisterDate(ToolForDateTime.StringDateToYearMonthDay(ToolForDateTime.LocalDateTimeToStringToDate(enterprise.getCreateTime())));

        }
        return ResponseWrapper.markSuccess(result);
    }

    /**
     * 登录用户修改自己的信息
     *
     * @param param 参数类
     */
    @Override
    public ResponseWrapper updateLoginUserInfo(UpdateLoginUserInfoParam param) {

        User user1 = userMapper.selectById(param.getAid());

        User user = new User();
        user.setAid(param.getAid());
        user.setName(param.getName());
        user.setPassword(user1.getPassword());
        user.setAccountNumber(user1.getAccountNumber());
        user.setPhone(param.getPhone());
        user.setEmail(param.getEmail());
        if (param.getSex().equals("男")){
            user.setGender(1);
        }else {
            user.setGender(0);
        }
        user.setIdentityNumber(param.getIdNumber());
        if (param.getIsMarried().equals("已婚")){
            user.setMaritalStatus(1);
        }else {
            user.setMaritalStatus(0);
        }
        user.setWorkExperience(user1.getWorkExperience());
        user.setIsAdmin(user1.getIsAdmin());
        user.setAddress(param.getAddress());
        user.setAvatar(param.getAvatar());

        int updateById = userMapper.updateById(user);
        if (updateById > 0){
            return ResponseWrapper.markCustomSuccess("修改成功！");
        }else {
            return ResponseWrapper.markCustomError("修改失败！");
        }
    }
}
